/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.autoconfigure.log;

import com.lvyh.lightframe.log.property.ThreadpoolProperties;
import com.lvyh.lightframe.log.thread.MDCThreadPoolTaskExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MDC asynchronous thread pool log tracking
 *
 * @author lvyh 2021/03/15.
 */
@Configuration
@ConditionalOnClass(MDCThreadPoolTaskExecutor.class)
@EnableConfigurationProperties(ThreadpoolProperties.class)
public class TraceThreadPoolTaskAutoConfigure {

    @Bean
    @ConditionalOnMissingBean(MDCThreadPoolTaskExecutor.class)
    @ConditionalOnProperty(prefix = "log.trace.async", value = "enabled", havingValue = "true")
    public MDCThreadPoolTaskExecutor mdcThreadPoolTaskExecutor(ThreadpoolProperties threadpoolProperties) {
        MDCThreadPoolTaskExecutor mdcThreadPoolTaskExecutor = new MDCThreadPoolTaskExecutor();
        mdcThreadPoolTaskExecutor.initialize();
        mdcThreadPoolTaskExecutor.setCorePoolSize(threadpoolProperties.getCorePoolSize());
        mdcThreadPoolTaskExecutor.setMaxPoolSize(threadpoolProperties.getMaxPoolSize());
        mdcThreadPoolTaskExecutor.setQueueCapacity(threadpoolProperties.getQueueCapacity());
        return mdcThreadPoolTaskExecutor;
    }

}
